package com.leilei.controller;

import com.google.gson.Gson;
import com.leilei.entity.CommodityVersion;
import com.leilei.service.ICommodityVersionService;
import com.leilei.service.impl.CommodityVersionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/CommodityVersionServlet")
public class CommodityVersionServlet extends BaseServlet{

    private ICommodityVersionService commodityVersionService = new CommodityVersionServiceImpl();

    public CommodityVersionServlet(){

    }

    protected void getCommodityVersionJson(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String spvid = req.getParameter("pvid");

        int pvid = spvid == null ? 0 : Integer.parseInt(spvid);

        CommodityVersion commodityVersion = commodityVersionService.getCommodityVersionByPvid(pvid);

        Gson gson = new Gson();
        resp.getWriter().write(gson.toJson(commodityVersion));

    }

    protected void getCommodityVersionByPvid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String spvid = req.getParameter("pvid");

        int pvid = spvid == null ? 0 : Integer.parseInt(spvid);

        CommodityVersion commodityVersion = commodityVersionService.getCommodityVersionByPvid(pvid);

        Gson gson = new Gson();
        resp.getWriter().write(gson.toJson(commodityVersion));
        req.setAttribute("commodityVersion", commodityVersion);
        req.getRequestDispatcher("jsp/updateCommodityVersion.jsp").forward(req, resp);

    }

    protected void saveCommodityVersion(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pid = Integer.parseInt(req.getParameter("pid"));
        String versionname = req.getParameter("versionname");
        String soprice = req.getParameter("oprice");
        String scprice = req.getParameter("cprice");
        String snum = req.getParameter("num");

        Double oprice = soprice == null ? 0 : Double.parseDouble(soprice);
        Double cprice = scprice == null ? 0 : Double.parseDouble(scprice);
        int num = snum == null ? 0 : Integer.parseInt(snum);

        CommodityVersion commodityVersion = new CommodityVersion();
        commodityVersion.setPid(pid);
        commodityVersion.setVersionname(versionname);
        commodityVersion.setOprice(oprice);
        commodityVersion.setCprice(cprice);
        commodityVersion.setNum(num);

        boolean result = commodityVersionService.insertCommodityVersion(commodityVersion);
        if (result){
            //跳转页面
            req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type=1").forward(req, resp);
        } else{
            //新增页面
            req.getRequestDispatcher("/jsp/insertCommodityVersion.jsp?pid="+pid).forward(req, resp);
        }

    }

    protected void deleteCommodityVersionByPvid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String spvid = req.getParameter("pvid");

        int pvid = spvid == null ? 0 : Integer.parseInt(spvid);

        boolean result = commodityVersionService.deleteCommodityVersionByPvid(pvid);
        if (result){
            //跳转页面

        } else{
            //新增页面

        }

    }

    protected void updateCommodityVersion(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pvid = Integer.parseInt(req.getParameter("pvid"));
        String versionname = req.getParameter("versionname");
        String soprice = req.getParameter("oprice");
        String scprice = req.getParameter("cprice");
        String snum = req.getParameter("num");

        double oprice = soprice == null ? 0.0 : Double.parseDouble(soprice);
        double cprice = scprice == null ? 0.0 : Double.parseDouble(scprice);
        int num = snum == null ? 0 : Integer.parseInt(snum);

        CommodityVersion commodityVersion = new CommodityVersion();
        commodityVersion.setPvid(pvid);
        commodityVersion.setVersionname(versionname);
        commodityVersion.setOprice(oprice);
        commodityVersion.setCprice(cprice);
        commodityVersion.setNum(num);

        boolean result = commodityVersionService.updateCommodityVersion(commodityVersion);
        if (result){
            //跳转页面
            req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type=1").forward(req, resp);
        } else{
            //新增页面
            req.getRequestDispatcher("/CommodityVersionServlet?method=getCommodityVersionByPvid&pvid=" + pvid).forward(req, resp);
        }

    }

}
